package com.key.web_admin.controller;

import com.alibaba.excel.EasyExcel;
import com.key.common.base.RespResult;
import com.key.modules.dto.CarrierPayDTO;
import com.key.modules.dto.CustomerStDTO;
import com.key.modules.dto.EditCustomerStDTO;
import com.key.modules.entity.CarrierSt;
import com.key.modules.entity.CustomerSt;
import com.key.modules.service.CustomerStService;
import com.key.modules.vo.CarrierPayVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/customerStatement")
public class CustomerStController {
    @Autowired
    CustomerStService customerStService;

    /**
     *查询信息
     * @return
     */
    @PostMapping("/findCustomerStatement")
    public RespResult findCustomerStatement(@RequestBody CustomerStDTO customerStDTO ){
        Map<String, Object> resultMap=customerStService.findCustomerStatement(customerStDTO);
        RespResult respResult =RespResult.success((Integer)resultMap.get("total"),resultMap.get("data"));
        return respResult;
    }

    /**
     * 导出excel
     * @return
     */
    @GetMapping("/exportData")

    public RespResult exportData(){
        //excel文件名
        String fileName = "D:\\\\客户对账单"+System.currentTimeMillis()+".xls";
        CustomerStDTO customerStDTO=new CustomerStDTO();
        customerStDTO.setCurrentPage(0);
        customerStDTO.setPageSize(1000);
        RespResult respResult=findCustomerStatement(customerStDTO);
        EasyExcel.write(fileName, CustomerSt.class).sheet("客户对账单").doWrite((ArrayList)respResult.getData());
        return RespResult.success("保存成功路径："+fileName);
    }

    /**
     * 取消对账单
     * @return
     */
    @GetMapping("/deleteStatement")
    public RespResult deleteStatement(String no){
        int i=customerStService.deleteStatement(no);
        return null;
    }

    /**
     *修改操作
     * @return
     */
    @PostMapping("/editStatement")
    public RespResult editStatement(@RequestBody EditCustomerStDTO editCustomerStDTO){
        CustomerSt customerSt=new CustomerSt();
        customerSt.setStatementNumber(editCustomerStDTO.getStatementNumber());
        String type=editCustomerStDTO.getType();
        if("回款确认".equals(type)){
            customerSt.setBillingStatus(2);
        }else if ("开票".equals(type)){
            customerSt.setInvoiceStatus(1);
        }else if("确认对账".equals(type)){
            customerSt.setBillingStatus(1);
        }
        int i=customerStService.editStatement(customerSt);
        if (i==0){
            return RespResult.error("不存在的参数");
        }else {
            return RespResult.success();
        }
    }
}
